import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:keyboard_actions/keyboard_actions.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

// ignore: slash_for_doc_comments
/**
 * 封装一个输入框widget
 */
// ignore: must_be_immutable
class TextFieldItem extends StatelessWidget {
  TextEditingController controller = TextEditingController();
  FocusNode focusNode = FocusNode();
  TextInputType keyboardType;
  String hintText;
  String labelText;
  KeyboardActionsConfig config;
  double height;
  bool obscureText = false;
  // String validator;
  FormFieldValidator validator;
  FormFieldSetter onSaved;
  Icon prefixIcon;
  IconButton suffixIcon;
  TextFieldItem({
    this.config,
    this.controller,
    this.focusNode,
    this.validator,
    this.keyboardType,
    this.hintText,
    this.height,
    this.onSaved,
    this.prefixIcon,
    this.suffixIcon,
    this.labelText,
    this.obscureText,
  });

  @override
  Widget build(BuildContext context) {
    //在flutter中iOS平台输入框，有兼容性问题，所以要特殊处理
    if (config != null && defaultTargetPlatform == TargetPlatform.iOS) {
      // 因Android平台输入法兼容问题，所以只配置IOS平台
      // ignore: non_constant_identifier_names
      var FormKeyboardActions;
      FormKeyboardActions.setKeyboardActions(context, config);
    }
    // ignore: non_constant_identifier_names
    return Container(
      //输入框总高度
      height: height,
      //画一个底部分割线
      decoration: BoxDecoration(
          border:
              Border(bottom: Divider.createBorderSide(context, width: 0.6))),
      child: Row(
        children: <Widget>[
          Expanded(
              flex: 1,
              child: TextFormField(
                //输入框控制器，controller可以获取文本
                controller: controller,
                //焦点控制
                focusNode: focusNode,

                ///验证方式
                validator: validator,
                //输入框类型
                keyboardType: keyboardType,

                ///保存表单
                onSaved: onSaved,
                obscureText : obscureText,
                //输入框下划线去掉
                style: TextStyle(fontSize: 18, color: Colors.white),
                ///字体样式
                decoration: InputDecoration(
                  hintText: hintText,
                  labelText:labelText,
                  prefixIcon: prefixIcon,
                  suffixIcon: suffixIcon,
                  labelStyle: TextStyle(
                      fontSize: ScreenUtil().setSp(32),
                      color: Colors.white), //设置文字样式
                  hintStyle: TextStyle(
                      fontSize: ScreenUtil().setSp(32),
                      color: Colors.white), //设置聚焦文字样式
                  helperStyle: TextStyle(
                      fontSize: ScreenUtil().setSp(28),
                      color: Colors.white), //设置帮助文字样式
                  errorStyle:
                      TextStyle(fontSize: ScreenUtil().setSp(26)), //设置错误文字样式
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(45),
                  ),
                  enabledBorder: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(45),
                    borderSide: BorderSide(color: Colors.white),
                  ),
                  focusedBorder: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(45),
                    borderSide: BorderSide(color: Colors.white),
                  ),
                ),
              )),
        ],
      ),
    );
  }
}
